


var totalnowsize2=0;
var targetbirdname2="";
var targetbirdfilename2="";
var folderbirdname2="";
var widthbird2=0;
var heightbird2=0;
var idmode2=0;
var arraydatamode2={};
//var arraytypefile={};
// ['zave','zavem', 'zavec', 'pdf'];

function checkBirdFiletypeexit(namefile,idindex){
var ext = namefile.split('.').pop().toLowerCase();
if($.inArray(ext,arraydatamode2[idindex]['checktype']) == -1) {
    alert('กรุณาเลือกประเภทไฟล์เอกสารเป็น PDF เท่านั้น');
    return false;
}else
{
    return true;
}
}

function handleBirdFile2Select(evt) {
    
         
    var f = evt.target.files[0]; // FileList object
    var indexset=evt.target.id.split("-");  
         
    if(checkBirdFiletypeexit(f.name,indexset[1])===false)
     {
     
     return false;
     }  
  
      
      datasendBirdajaxFile(f,indexset[1]);

  
}

function resizeBirdimgFile(file,idindex){

      
        sendBirdfiledataFile(file,idindex);
      
     
    

}

function handleFilesImageSet(filesset,evt) {
    
    var f = filesset['target']['files'][0];
    var indexset= filesset['target']['id'].split("-"); 
     if(checkBirdFiletypeexit(f.name,indexset[1])===false)
     {
     
     return false;
     }  
     

          
         datasendBirdajaxFile(f,indexset[1]);

}

function sendBirdfiledataFile(f,idindex){


        var ext = f.name.split('.').pop().toLowerCase();
        if(arraydatamode2[idindex][ext]==0)
        {
        arraydatamode2[idindex][ext]=1;
        
        var xmlhttp;
        
        if(window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLDOM");

        
        $("#"+ext+"fileload-"+idindex).show();
        $("#"+ext+"fileload-"+idindex).css("height",0);
        $("#"+ext+"filetarget-"+idindex).html(ext.toUpperCase()); 
        
        
        
        
        
        
       
        
        xmlhttp.upload.onprogress = function(e) {
        
          if (e.lengthComputable) {

         var loaded = (e.loaded / e.total)*81;
         

        $("#"+ext+"fileload-"+idindex).css("height",parseInt(loaded,10));
                           
         
         
         }
     
        }

        xmlhttp.upload.onload = function(e) {
            

        }

        xmlhttp.onreadystatechange = function() {
        
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                var myObject = eval('(' + xmlhttp.responseText + ')');

                if(arraydatamode2[idindex]['mutiplefile'])
                {
                $('#'+ext+'file2birdsavename-'+idindex).val(myObject.filename);
                $("#"+ext+"filetarget-"+idindex).html('<a href="'+myObject.filelink+'" target="_blank">'+ext.toUpperCase()+'</a>');
                
                

                
                
                 if(ext=="jpg"||ext=="png"||ext=="jpeg")
                {
                $('#file2imgbirdsavename-'+idindex).val(myObject.filename);
                $("#imgfiletarget-"+idindex).html('<img id="imgsrcfiletarget-'+idindex+'" src="'+myObject.filelink+'" class="upThumImg" >');
                                            
                }
                
                
                }else
                {
                
             
                if(ext=="pdf"||ext=="mp4")
                {
                $('#file2birdsavename-'+idindex).val(myObject.filename);
                $("#filetarget-"+idindex).html('<a href="'+myObject.filelink+'" target="_blank">'+ext.toUpperCase()+'</a>');
                arraydatamode2[idindex]['working2']=0;
                
                }
                else if(ext=="jpg"||ext=="png"||ext=="jpeg")
                {
                $('#file2imgbirdsavename-'+idindex).val(myObject.filename);
               
                arraydatamode2[idindex]['working1']=0;
                $("#imgfiletarget-"+idindex).html('<img id="imgsrcfiletarget-'+idindex+'" src="'+myObject.filelink+'" class="upThumImg" >');
                
                
                }
                
                
                
                
                }
                //console.log('check finish--->#file2imgbirdsavename-'+idindex);
               // console.log('check finish2--->'+$('#file2imgbirdsavename-'+idindex).val());
                
                arraydatamode2[idindex][ext]=0;
                
                $("#"+ext+"fileload-"+idindex).css("height",81);

            
            }
          
          
        }
        if(serviceinfo)
        {
        xmlhttp.open("post", serviceinfo+"uploadfile", true);
        }
        
        xmlhttp.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
        xmlhttp.setRequestHeader("Cache-Control", "no-cache");

        var formData = new FormData();

        formData.append("Filedata", f);

        formData.append("typefile", ext);
        

        xmlhttp.send(formData);
        
        return xmlhttp;
        }else
        {
        
        alert("รอโหลดไฟล์ "+ext+" ให้เสร็จก่อน");
        
        }

}

function datasendBirdajaxFile(f,idindex){

    // if(f.size>500000)
//        {
//        resizeBirdimg(f,idindex);
//        return false;
//        }else
//        {
//        sendBirdfiledata(f,idindex);
//        
//        }  
        
        resizeBirdimgFile(f,idindex);  
   

}

function dropBirdFile(evt) {
     evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files;
    var count = files.length;

    //console.log(evt);
    //console.log();

    if(checkBirdFiletypeexit(files[0].name,arraydatamode2[evt.currentTarget.id])===false)
     {
     
     return false;
     }
 
 
    // dataBirdsendajax(files[0],indexset[1]);
    
    
    datasendBirdajaxFile(files[0],arraydatamode2[evt.currentTarget.id]);
}

function handleBirdFiles2(filesset,evt) {
     // console.log(files);
      var f = filesset['target']['files'][0];
      var indexset= filesset['target']['id'].split("-");  
      
     if(checkBirdFiletypeexit(f.name,indexset[1])===false)
     {
     
     return false;
     }  
     console.log(f.name);
     $('#pdf-name-'+indexset[1]).val(f.name);
        // console.log(evt);
         
         datasendBirdajaxFile(f,indexset[1]);
}
function checkloadfunction(f,indexcheck)
{

var ext = f.name.split('.').pop().toLowerCase();
if(arraydatamode2[indexcheck][ext]==1)
{


return checkloadfunction(f,indexcheck);


}else
{

return true;

}


}
var lastcheckid='test';
function handleBirdFiles3(filesset,evt) {
     
     
     $.each( filesset['target']['files'], function( key, value ) {
     
     
        var f=value;
        
         if(checkBirdFiletypeexit(f.name,'0')===false)
         {
        //console.log('test no');
        
        
        }  else
        {
        
        var ext = f.name.split('.').pop().toLowerCase();
        
        
        
        
        
        if(ext=='pdf')
        {
        
        var krowcheck=0;
        var indexcheck=0;
        
        $( 'input[name="pdf1[]"]' ).each(function( index ) {
        
        var testcheck=$( this ).attr('id');
        if($('#'+testcheck).val()=='')
        {
     //   console.log('test check');
     
        
        
        
        indexcheck=testcheck.split('-').pop();
        
        if(arraydatamode2[indexcheck][ext]==0&&$('#'+testcheck).val()=='')
        {
         if(arraydatamode2[indexcheck]['working2']==0)
        {
        //console.log('Hello id---->'+testcheck);
        //console.log('Index-->'+indexcheck);
        //console.log('empty--->'+$('#'+testcheck).val());
        //console.log('dataindex--->'+arraydatamode2[indexcheck][ext]);
        krowcheck=1;
        
        return false;
        }
        
        }
        
        }
        
        
         
        });
        
        
        if(krowcheck==0)
        {
        
        addpdfform();
        indexcheck=idmode2-1;
        lastcheckid=indexcheck+'';
        arraydatamode2[indexcheck]['working2']=1;
        
        
     //   var arrname=f.name.split('.')
        
        
        

         var res = f.name.replace(".pdf", ""); 
         $('#pdf-name-'+indexcheck).val(res);
        
        
        sendBirdfiledataFile(f,indexcheck);
        
       // console.log(1);
//        console.log(arraydatamode2[indexcheck]);
//        console.log('test--->'+indexcheck);
//        console.log('test2--->'+arraydatamode2[indexcheck][ext]);
        
        
        }else
        {
        var arrname=f.name.split('.')
        
        
        
        
        $('#pdf-name-'+indexcheck).val(arrname[0]);
        //console.log(2);
//        console.log(arraydatamode2[indexcheck]);
//        console.log('test--->'+indexcheck);
//        console.log('test2--->'+arraydatamode2[indexcheck][ext]);
        arraydatamode2[indexcheck]['working2']=1;
        sendBirdfiledataFile(f,indexcheck);
        
        }
        
        
        
        
        }
        
        else
        {
        
        var krowcheck=0;
        var indexcheck=0;
        //console.log($( 'input[name="imgpdf1[]"]' ).length);
        $( 'input[name="imgpdf1[]"]' ).each(function( index ) {
        
        var testcheck=$( this ).attr('id');
        indexcheck=testcheck.split('-').pop();
     //  console.log('run-->'+indexcheck+' file-->'+$('#'+testcheck).val());
        if($('#'+testcheck).val()=='')
        {
     //   console.log('test check');
     
        
        
        
        
        
        
        
        
        
        
        
        
        
      
       
        
        
        if(arraydatamode2[indexcheck][ext]==0&&$('#'+testcheck).val()=='')
        {
        
        
        
        if(arraydatamode2[indexcheck]['working1']==0)
        {
        //console.log('Hello id---->'+testcheck);
        //console.log('Index-->'+indexcheck);
        //console.log('empty--->'+$('#'+testcheck).val());
        //console.log('dataindex--->'+arraydatamode2[indexcheck][ext]);
        krowcheck=1;
        
        return false;
        }
        
        }
        
        
        
        
        
        
        
        
        
        }
        
        
         
        });
        
        
        if(krowcheck==0)
        {
        
        
        addpdfform();
        indexcheck=idmode2-1;
        
        
        arraydatamode2[indexcheck]['working1']=1;
        sendBirdfiledataFile(f,indexcheck);
        
        
        
      //  console.log(1);
//        console.log(arraydatamode2[indexcheck]);
//        console.log('test--->'+indexcheck);
//        console.log('test2--->'+arraydatamode2[indexcheck][ext]);

        
        
        
        }else
        {
        
        
        
        
        
        arraydatamode2[indexcheck]['working1']=1;
       // console.log(2);
//        console.log(arraydatamode2[indexcheck]);
//        
//        console.log('test--->'+indexcheck);
//        console.log('test2--->'+arraydatamode2[indexcheck][ext]);
        
        
        sendBirdfiledataFile(f,indexcheck);
        
        }
        
        
        
        
        }
        
        
        //console.log('test yes');
        
        }

        
     
     
     
     });
     
     
     
     return true;
      var f = filesset['target']['files'][0];
      var indexset= filesset['target']['id'].split("-");  
      
     if(checkBirdFiletypeexit(f.name,indexset[1])===false)
     {
     
     return false;
     }  
     
        // console.log(evt);
         
         datasendBirdajaxFile(f,indexset[1]);
}

function dragBirdFileEnter(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}

function dragBirdFileExit(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}

function dragBirdFileOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}

function configBirdFile(targetid,arraytype,namefile,accepttype,btnadd,mutiplefile,arraytype2){
      

        //$("#"+targetbirdname).attr("id",targetbirdname+"-"+targetid)
       var dropbox = document.getElementById(targetid);
       var nowid =idmode2;
       dropbox.addEventListener("drop", dropBirdFile, false);
       dropbox.addEventListener("dragenter", dragBirdFileEnter, false);
       dropbox.addEventListener("dragexit", dragBirdFileExit, false);
       dropbox.addEventListener("dragover", dragBirdFileOver, false);
    
       
       arraydatamode2[idmode2]={"target":targetid,"checktype":arraytype,"mutiplefile":mutiplefile};
       
       arraydatamode2[targetid]=idmode2;
       var acceptfile="";
       if(accepttype)
       {
       acceptfile='accept="'+accepttype+'"';
       }
       
       $("#"+targetid).before($('<input  '+acceptfile+'  value="1" name="filebirdfile" type="file" id="filebirdfile-'+idmode2+'" style="display: none;"  >'));
       if(mutiplefile)
       {
       for (index in arraytype2) {
       var  value = arraytype2[index];
       $("#"+targetid).before($('<input name="'+namefile+'"  type="hidden" id="'+value+'file2birdsavename-'+idmode2+'"   >'));
       
       }
       
       
      for (index in arraytype) {
      var  value = arraytype[index]; 
       arraydatamode2[idmode2][value]=0;
      }
       
       $("#"+targetid).before($('<input name="img'+namefile+'"  type="hidden" id="file2imgbirdsavename-'+idmode2+'"   >'));
       }else
       {
       $("#"+targetid).before($('<input name="'+namefile+'"  type="hidden" id="file2birdsavename-'+idmode2+'"   >'));
       $("#"+targetid).before($('<input name="img'+namefile+'"  type="hidden" id="file2imgbirdsavename-'+idmode2+'"   >'));
       var typeindex=0;
       for(index2 in arraytype)
       {
       var  value = arraytype[index2]; 
       arraydatamode2[idmode2][value]=0;
       typeindex++;
       
       }
       
       
       arraydatamode2[idmode2]['working1']=0;
       arraydatamode2[idmode2]['working2']=0;
       
      // arraydatamode2[idmode2][arraytype[1]]=0;
       //arraydatamode2[idmode2][arraytype[2]]=0;
       }
       
       
       document.getElementById('filebirdfile-'+idmode2).addEventListener('change', handleBirdFiles2, false);
       var  filebirdsettrigger=  'filebirdfile-'+idmode2;
       //console.log(btnadd);
       $("."+btnadd).click(function() {
       
               // console.log(filebirdsettrigger);
               $("#"+filebirdsettrigger).trigger('click');
                //console.log(idmode);
//                
       });
       
     //  console.log(arraydatamode);
        idmode2++; 
        return nowid;  

}
